package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private MovementService movementService;
    @DubboReference
    private CommentService commentService;
    @DubboReference
    private VideoService videoService;

    // 分页查询用户列表
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
        //调用rpc查询所有用户信息并分页
        PageBeanVo pageBeanVo=userInfoService.findUserInfoByPage(pageNum,pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findUserInfoByUserId(Long userId) {
        //调用rpc根据用户id查询用户详情
        UserInfo userInfo=userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    // 动态分页查询
    public ResponseEntity findMovementVoByPage(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        //根据rpc查询动态分页类
        PageBeanVo pageBeanVo=movementService.findMovementVoByPage(userId,state,pageNum,pageSize);
        //创建vo集合
        List<MovementVo> movementVos=new ArrayList<>();
        //取出动态集合
        List<Movement> movements= (List<Movement>) pageBeanVo.getItems();
        //判断非空遍历
        if (CollectionUtil.isNotEmpty(movements)) {
            for (Movement movement : movements) {
                //创建vo类 赋值并保存
                MovementVo movementVo=new MovementVo();
                movementVo.setUserInfo(userInfoService.findById(movement.getUserId()));
                movementVo.setMovement(movement);
                movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
                movementVos.add(movementVo);
            }
        }
        //把vo集合赋值给动态分页类
        pageBeanVo.setItems(movementVos);
        //返回
        return ResponseEntity.ok(pageBeanVo);
    }

    // 动态详情查询
    public ResponseEntity findMovementVoById(String publishId) {
        //调用rpc根据动态主键查询动态详情
         Movement movement=movementService.findById(new ObjectId(publishId));
         //创建vo类 赋值
         MovementVo movementVo=new MovementVo();
         movementVo.setUserInfo(userInfoService.findById(movement.getUserId()));
         movementVo.setMovement(movement);
         movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
         //返回
        return ResponseEntity.ok(movementVo);
    }

    // 动态评论分页查询
    public ResponseEntity findCommentVoByPage(Integer pageNum, Integer pageSize, String publishId) {
        //调用rpc查询动态评论分页
        PageBeanVo pageBeanVo=commentService.findCommentByPage(publishId,pageNum,pageSize,2);
        //创建vo集合
        List<CommentVo> commentVos=new ArrayList<>();
        //取出动态评论集合
        List<Comment> comments= (List<Comment>) pageBeanVo.getItems();
        //判断非空 遍历
        if (CollectionUtil.isNotEmpty(comments)) {
            for (Comment comment : comments) {
                //创建 vo类 赋值并保存
                CommentVo commentVo=new CommentVo();
                UserInfo userInfo=userInfoService.findById(comment.getUserId());
                commentVo.setId(comment.getId().toHexString());
                commentVo.setAvatar(userInfo.getAvatar());
                commentVo.setNickname(userInfo.getNickname());
                commentVo.setContent(comment.getContent());
                commentVo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));
                commentVo.setLikeCount(comment.getLikeCount());
                commentVos.add(commentVo);
            }
        }
        //把vo集合赋值到动态分页类
        pageBeanVo.setItems(commentVos);
        //返回
        return ResponseEntity.ok(pageBeanVo);
    }

    // 查询指定用户id分页视频
    public ResponseEntity findVideoVoByPage(Integer pageNum, Integer pageSize, Long userId) {
        //调用rpc根据userId查询视频并分页
        PageBeanVo pageBeanVo=videoService.findVideoVoByUserIdPage(userId,pageNum,pageSize);
        //创建vo集合
        List<VideoVo> videoVos=new ArrayList<>();
        //取出视频集合
        List<Video> videos= (List<Video>) pageBeanVo.getItems();
        //判断非空 遍历
        if (CollectionUtil.isNotEmpty(videos)) {
            for (Video video : videos) {
                //创建vo类 赋值并保存
                VideoVo videoVo=new VideoVo();
                videoVo.setUserInfo(userInfoService.findById(video.getUserId()));
                videoVo.setVideo(video);
                videoVos.add(videoVo);
            }
        }
        //把vo集合赋值到视频分页类
        pageBeanVo.setItems(videoVos);
        //返回
        return ResponseEntity.ok(pageBeanVo);
    }
}
